<template>
  <div class="box">
    <div class="box-header" :title="hintText">
      <span class="title pos-left">{{title}}</span>
      <ul class="tools pos-right">
        <li class="item">
          <button class="btn" type="button" @click="showSettingWindow">
            <i class="icon icon-setting"></i>
          </button>
        </li>
      </ul>
    </div>
    <div class="box-body">
      <div class="chart fullsize" id="chart-3"></div>
      <div class="chartSetting" v-show="showSetting">
        <div class="form">
          <div class="form-head">
            <div class="title">选择时间范围</div>
          </div>
          <div class="form-body">
            <div class="form-item">
              <div class="label">开始时间</div>
              <div class="field">
                <input
                  type="date"
                  class="datepicker"
                  v-model="startTime"
                  :max="timeLimit"
                  :min="timeMin"
                >
              </div>
            </div>
            <div class="form-item">
              <div class="label">结束时间</div>
              <div class="field">
                <input
                  type="date"
                  class="datepicker"
                  v-model="endTime"
                  :max="timeLimit"
                  :min="timeMin"
                >
              </div>
            </div>
          </div>
          <div class="form-foot">
            <button type="button" @click="saveSetting" class="btn bg-green">
              <span class="text">确定</span>
            </button>
            <button type="button" @click="cancelSetting" class="btn bg-green">
              <span class="text">关闭</span>
            </button>
          </div>
        </div>
      </div>
      <x-loading :show="showLoading"></x-loading>
      <dl-Merge ref="dialog" :show="showModal" :take="keyModal"></dl-Merge>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import echarts from "echarts";
import Loading from "@/components/Loading";
import Pager from "@/components/Pager";
import dlMerge from "@/view/oldonepic/dl_Merge";

export default {
  data() {
    return {
      startTime: this.dateShift(-30),
      endTime: this.today(),
      timeLimit: this.today(),
      timeMin: this.dateShift(-365*2),
      showSetting: false,
      showLoading: false,
      chartInstance: null,
      option: null,
      params: null,
      showModal: false,
      keyModal: null
    };
  },
  props: {
    title: String
  },
  components: {
    "x-loading": Loading,
    "x-pager": Pager,
    "dl-Merge": dlMerge
  },
  computed: {
    hintText() {
      return `显示数据自${this.startTime}起，截止至${this.endTime}`;
    }
  },
  mounted() {
    this.renderChart();
  },
  methods: {
    showSettingWindow() {
      this.showSetting = true;
    },
    cancelSetting() {
      this.showSetting = false;
    },
    saveSetting() {
      this.renderChart();
      this.showSetting = false;
    },
    renderChart() {
      let $this = this;
      let params = {
        startTime: this.startTime + " 00:00:00",
        endTime: this.endTime + " 23:59:59",
        HTTEST: "test"
      };
      this.showLoading = true;
      axios
        .get(this.URLHEAD + "YZT_XFJQTZ", {
          params: params
        })
        .then(res => {
          let data = res.data;
         this.option = {
            tooltip: {
              trigger: "axis",
              axisPointer: {
                lineStyle: {
                  color: "#57617B"
                }
              },
              formatter:'{b0}:{a0}时间段接警{c0}次<br />{b1}:{a1}时间段接警{c1}次<br />{b2}:{a2}时间段接警{c2}次<br />{b3}:{a3}时间段接警{c3}次'
            },
            grid: {
              left: "3%",
              right: "4%",
              bottom: "3%",
              containLabel: true
            },
            legend: {
              top: "top",
              left: "left",
              data: ["凌晨(0:00-7:00)", "上午(7:00-12:00)", "下午(12:00-18:00)", "晚上(18:00-24:00)"],
              textStyle: {
                color: "#fff",
                fontSize: 12
              }
            },
            xAxis: [
              {
                type: "category",
                boundaryGap: false,
                axisLine: {
                  lineStyle: {
                    color: "rgba(255,255,255,0.5)"
                  }
                },
                data: data.Data.map(item => {
                  return item.name;
                })
              }
            ],
            yAxis: [
              {
                type: "value",
                name: "警情",
                axisTick: {
                  show: false
                },
                axisLine: {
                  lineStyle: {
                    color: "rgba(255,255,255,0.3)"
                  }
                },
                axisLabel: {
                  margin: 10,
                  textStyle: {
                    fontSize: 14,
                    color: "rgba(255,255,255,0.3)"
                  },
                  formatter: "{value}次"
                },
                splitLine: {
                  lineStyle: {
                    color: "rgba(255,255,255,0.3)"
                  }
                }
              }
            ],
            series: [
              {
                name: "凌晨(0:00-7:00)",
                type: "line",
                smooth: false,
                symbol: "circle",
                symbolSize: 5,
                showSymbol: true,
                lineStyle: {
                  normal: {
                    width: 3
                  }
                },
                label: {
                  normal: {
                    show: true,
                    position: "top",
                    color: "rgba(255,255,255,1)"
                  }
                },
                areaStyle: {
                  normal: {
                    color: new echarts.graphic.LinearGradient(
                      0,
                      0,
                      0,
                      1,
                      [
                        {
                          offset: 0,
                          color: "rgba(205,52,42, 0.5)"
                        },
                        {
                          offset: 0.8,
                          color: "rgba(235,235,21, 0)"
                        }
                      ],
                      false
                    ),
                    shadowColor: "rgba(0, 0, 0, 0.1)",
                    shadowBlur: 10
                  }
                },
                itemStyle: {
                  normal: {
                    color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                      {
                        offset: 0,
                        color: "rgba(235,235,21,1)"
                      },
                      {
                        offset: 1,
                        color: "rgba(235,235,21,1)"
                      }
                    ])
                  },
                  emphasis: {
                    color: "rgb(99,250,235)",
                    borderColor: "rgba(99,250,235,0.2)",
                    extraCssText: "box-shadow: 8px 8px 8px rgba(0, 0, 0, 1);",
                    borderWidth: 10
                  }
                },
                data: data.Data.map(item => {
                  return item.lc;
                })
              },
              {
                name: "上午(7:00-12:00)",
                type: "line",
                smooth: false,
                symbol: "circle",
                symbolSize: 5,
                showSymbol: true,
                lineStyle: {
                  normal: {
                    width: 3
                  }
                },
                label: {
                  normal: {
                    show: true,
                    position: "top",
                    color: "rgba(255,255,255,1)"
                  }
                },
                areaStyle: {
                  normal: {
                    color: new echarts.graphic.LinearGradient(
                      0,
                      0,
                      0,
                      1,
                      [
                        {
                          offset: 0,
                          color: "rgba(1, 148, 224, 0.5)"
                        },
                        {
                          offset: 0.8,
                          color: "rgba(5, 169, 255, 0)"
                        }
                      ],
                      false
                    ),
                    shadowColor: "rgba(0, 0, 0, 0.1)",
                    shadowBlur: 10
                  }
                },
                itemStyle: {
                  normal: {
                    color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                      {
                        offset: 0,
                        color: "rgba(12, 255, 224,1)"
                      },
                      {
                        offset: 1,
                        color: "rgba(140, 199, 255,1)"
                      }
                    ])
                  },
                  emphasis: {
                    color: "rgb(99,250,235)",
                    borderColor: "rgba(99,250,235,0.2)",
                    extraCssText: "box-shadow: 8px 8px 8px rgba(0, 0, 0, 1);",
                    borderWidth: 10
                  }
                },
                data: data.Data.map(item => {
                  return item.sw;
                })
              }
              ,
              {
                name: "下午(12:00-18:00)",
                type: "line",
                smooth: false,
                symbol: "circle",
                symbolSize: 5,
                showSymbol: true,
                lineStyle: {
                  normal: {
                    width: 3
                  }
                },
                label: {
                  normal: {
                    show: true,
                    position: "top",
                    color: "rgba(255,255,255,1)"
                  }
                },
                areaStyle: {
                  normal: {
                    color: new echarts.graphic.LinearGradient(
                      0,
                      0,
                      0,
                      1,
                      [
                        {
                          offset: 0,
                          color: "rgba(1, 148, 224, 0.5)"
                        },
                        {
                          offset: 0.8,
                          color: "rgba(5, 169, 255, 0)"
                        }
                      ],
                      false
                    ),
                    shadowColor: "rgba(0, 0, 0, 0.1)",
                    shadowBlur: 10
                  }
                },
                itemStyle: {
                  normal: {
                    color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                      {
                        offset: 0,
                        color: "rgba(241, 57, 8,1)"
                      },
                      {
                        offset: 1,
                        color: "rgba(248, 101, 61,1)"
                      }
                    ])
                  },
                  emphasis: {
                    color: "rgb(99,250,235)",
                    borderColor: "rgba(99,250,235,0.2)",
                    extraCssText: "box-shadow: 8px 8px 8px rgba(0, 0, 0, 1);",
                    borderWidth: 10
                  }
                },
                data: data.Data.map(item => {
                  return item.xw;
                })
              }
              ,
              {
                name: "晚上(18:00-24:00)",
                type: "line",
                smooth: false,
                symbol: "circle",
                symbolSize: 5,
                showSymbol: true,
                lineStyle: {
                  normal: {
                    width: 3
                  }
                },
                label: {
                  normal: {
                    show: true,
                    position: "top",
                    color: "rgba(255,255,255,1)"
                  }
                },
                areaStyle: {
                  normal: {
                    color: new echarts.graphic.LinearGradient(
                      0,
                      0,
                      0,
                      1,
                      [
                        {
                          offset: 0,
                          color: "rgba(1, 148, 224, 0.5)"
                        },
                        {
                          offset: 0.8,
                          color: "rgba(5, 169, 255, 0)"
                        }
                      ],
                      false
                    ),
                    shadowColor: "rgba(0, 0, 0, 0.1)",
                    shadowBlur: 10
                  }
                },
                itemStyle: {
                  normal: {
                    color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                      {
                        offset: 0,
                        color: "rgba(1, 148, 224,1)"
                      },
                      {
                        offset: 1,
                        color: "rgba(5, 169, 255,1)"
                      }
                    ])
                  },
                  emphasis: {
                    color: "rgb(99,250,235)",
                    borderColor: "rgba(99,250,235,0.2)",
                    extraCssText: "box-shadow: 8px 8px 8px rgba(0, 0, 0, 1);",
                    borderWidth: 10
                  }
                },
                data: data.Data.map(item => {
                  return item.ws;
                })
              }
            ]
          };
          this.chartInstance = this.$Crender("chart-3", this.option);
          this.chartInstance.on("click", e => {
            //console.log(e);
            let selectTime = e.name;
            let type=e.seriesName.split("(")[0];
            $this.keyModal = selectTime+","+type;
            $this.$refs.dialog.query(selectTime+","+type);
          });
         this.showLoading = false;
        });
    }
  }
};
</script>
<style scoped>
</style>
